package com.qsxc.dao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.qsxc.business.bean.R;
import com.qsxc.business.promoter.bean.vo.DivideRecordVo;
import com.qsxc.dao.entity.SmPromoter;
import com.qsxc.dao.entity.SmPromoterDivideRecord;
import com.qsxc.dao.entity.SmUserPayRecord;
import com.qsxc.dao.mapper.SmPromoterDivideRecordMapper;
import com.qsxc.dao.mapper.SmPromoterMapper;
import com.qsxc.dao.service.ISmPromoterDivideRecordService;
import com.qsxc.utils.CollectionUtil;
import com.qsxc.utils.validator.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 推广员分成记录表，支付成功后生成 服务实现类
 * </p>
 *
 * @author
 * @since 2024-09-25
 */
@Slf4j
@Service
public class SmPromoterDivideRecordServiceImpl extends ServiceImpl<SmPromoterDivideRecordMapper, SmPromoterDivideRecord> implements ISmPromoterDivideRecordService {
    @Resource
    private SmPromoterMapper smPromoterMapper;
    @Resource
    private SmPromoterDivideRecordMapper smPromoterDivideRecordMapper;

    @Override
    public R<List<DivideRecordVo>> getPromoterDivideRecord(String openid, String cashType) throws Exception {
        // 根据openid查询推广员信息
        SmPromoter smPromoter = smPromoterMapper.queryByOpenid(openid);
        Assert.isNull(smPromoter, "推广员信息不存在");

        Long promoterId = smPromoter.getId();
        // 根据推广员id查询分成记录列表
        List<DivideRecordVo> divideRecordVoList = smPromoterDivideRecordMapper.queryListByPromoterIdAndCashType(promoterId, cashType);
        if (CollectionUtil.isEmpty(divideRecordVoList)) {
            return R.success();
        }
        
        return R.success(divideRecordVoList);
    }

    @Override
    public String createBySmDdjdServicePayRecord(SmUserPayRecord payRecord) {
        if (payRecord.getPromoterId() == null) {
            log.info("====== 推广员id不存在 {}======", payRecord);
            return null;
        }
        // 查询推广员信息
        SmPromoter smPromoter = smPromoterMapper.queryById(String.valueOf(payRecord.getPromoterId()));
        if (smPromoter == null) {
            log.info("====== 推广员不存在 {}======", payRecord);
            return null;
        }
        // 判断比例设置是否合理
        if (smPromoter.getRateStudent() == null) {
            log.info("====== 学员分成比例不存在 {}======", payRecord);
            return null;
        }
        if (smPromoter.getRateStudent() < 0 || smPromoter.getRateStudent() > 100) {
            log.info("====== 学员分成比例不合理 {}======", payRecord);
            return null;
        }

        if (smPromoter.getRateStudent() + smPromoter.getRateHigher() > 100) {
            log.info("====== 学员分成比例不合理 {}======", payRecord);
            return null;
        }

        BigDecimal bigDecimal1 = new BigDecimal(smPromoter.getRateStudent());
        BigDecimal bigDecimal2 = new BigDecimal(smPromoter.getRateHigher());
        BigDecimal bigDecimal3 = new BigDecimal(100.0);
        BigDecimal bigDecimal = payRecord.getAmountMoney().multiply(bigDecimal1.divide(bigDecimal3));
        // 上级
        BigDecimal heightBD = payRecord.getAmountMoney().multiply(bigDecimal2.divide(bigDecimal3));
//        BigDecimal heightBD = bigDecimal.multiply(bigDecimal2.divide(bigDecimal3));
        // 自己
//        BigDecimal selfBD = bigDecimal.subtract(heightBD);
        BigDecimal selfBD = bigDecimal;
        // 包含上级分成比例
        if (null != smPromoter.getPId()) {
            // 查询推广员信息
            SmPromoter smPromoter1 = smPromoterMapper.queryById(String.valueOf(smPromoter.getPId()));
            if (smPromoter1 == null || smPromoter.getRateHigher() == null) {
                // 自己-上级不存在全给自己
                SmPromoterDivideRecord smPromoterDivideRecord1 = new SmPromoterDivideRecord();
                smPromoterDivideRecord1.setCreateBy(payRecord.getUserId());
                smPromoterDivideRecord1.setCreateTime(new Date());
                smPromoterDivideRecord1.setUserId(payRecord.getUserId());
                smPromoterDivideRecord1.setPromoterId(smPromoter.getId());
                smPromoterDivideRecord1.setPayRecordId(payRecord.getId());
                smPromoterDivideRecord1.setAmountMoney(bigDecimal.doubleValue());
                smPromoterDivideRecord1.setSourceType("1");
                smPromoterDivideRecord1.setStatus("1");
                smPromoterDivideRecordMapper.insert(smPromoterDivideRecord1);
            }else {
                if (smPromoter.getRateHigher() < 0 || smPromoter.getRateHigher() > 100) {
                    log.info("====== 上级分成比例不合理 {}======", payRecord);
                    return null;
                }
                // 自己
                SmPromoterDivideRecord smPromoterDivideRecord1 = new SmPromoterDivideRecord();
                smPromoterDivideRecord1.setCreateBy(payRecord.getUserId());
                smPromoterDivideRecord1.setCreateTime(new Date());
                smPromoterDivideRecord1.setUserId(payRecord.getUserId());
                smPromoterDivideRecord1.setPromoterId(smPromoter.getId());
                smPromoterDivideRecord1.setPayRecordId(payRecord.getId());
                smPromoterDivideRecord1.setAmountMoney(selfBD.doubleValue());
                smPromoterDivideRecord1.setSourceType("1");
                smPromoterDivideRecord1.setStatus("1");
                smPromoterDivideRecordMapper.insert(smPromoterDivideRecord1);

                // 上级
                SmPromoterDivideRecord smPromoterDivideRecord = new SmPromoterDivideRecord();
                smPromoterDivideRecord.setCreateBy(payRecord.getUserId());
                smPromoterDivideRecord.setCreateTime(new Date());
                smPromoterDivideRecord.setUserId(payRecord.getUserId());
                smPromoterDivideRecord.setPromoterId(smPromoter1.getId());
                smPromoterDivideRecord.setPayRecordId(payRecord.getId());
                smPromoterDivideRecord.setSubPromoterId(smPromoter.getId());
                smPromoterDivideRecord.setSourceType("2");
                // 上级推广员获取当前推广员的比例 学员缴费200 A50% B50% A、B各分50
                smPromoterDivideRecord.setAmountMoney(heightBD.doubleValue());
                smPromoterDivideRecord.setStatus("1");
                smPromoterDivideRecordMapper.insert(smPromoterDivideRecord);
            }
        }else {
            // 自己-上级不存在全给自己
            SmPromoterDivideRecord smPromoterDivideRecord = new SmPromoterDivideRecord();
            smPromoterDivideRecord.setCreateBy(payRecord.getUserId());
            smPromoterDivideRecord.setCreateTime(new Date());
            smPromoterDivideRecord.setUserId(payRecord.getUserId());
            smPromoterDivideRecord.setPromoterId(smPromoter.getId());
            smPromoterDivideRecord.setPayRecordId(payRecord.getId());
            smPromoterDivideRecord.setAmountMoney(bigDecimal.doubleValue());
            smPromoterDivideRecord.setSourceType("1");
            smPromoterDivideRecord.setStatus("1");
            smPromoterDivideRecordMapper.insert(smPromoterDivideRecord);
        }
        return null;
    }

}
